package test.spider;

import java.io.File;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import test.common.Config;
import test.common.util.FileUtil;
import test.net.DownLoadFile;
import test.net.http.DownLoadUtil;
import test.net.http.HtmlParserTool;
import test.net.http.MessageBean;
import test.spider.frontier.Frontier;

public class ChildThread extends Thread {
	private Log logger = LogFactory.getLog(ChildThread.class);
	public static int thread = 0;
	private Frontier unvisitFrontier;
	private Frontier visitedFrontier;

	public ChildThread(Frontier unvisitFrontier, Frontier visitedFrontier) {
		this.unvisitFrontier = unvisitFrontier;
		this.visitedFrontier = visitedFrontier;
	}

	static LinkFilter filter = new LinkFilter() {
		public boolean accept(String url) {
			if (url.contains("www.qq.com")
					|| url.contains("jpg")||url.contains("png")||url.contains("gif"))
				return true;
			else
				return false;
		}
	};

	@Override
	public void run() {
		logger.info(Thread.currentThread().getName());
		CrawlUrl url;
		try {
			while ((url = dequeueUrl()) != null) {
					MessageBean mb = DownLoadUtil.downloadGet(url.getOriUrl());
					String rootPath = Config.getProperty("rootPath");
					String filename = DownLoadUtil.getFileNameByUrl(url.getOriUrl(),mb.getM_content_type());
					
					FileUtil.saveFile(mb.getM_content(), rootPath+filename);
					Set<String> links = HtmlParserTool.extracLinks(url
							.getOriUrl(), filter);
					addUrls(unvisitFrontier, links);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void addUrls(Frontier forntier, Set<String> links) throws Exception {
		for (String unvisited : links) {
			CrawlUrl crawUrl = new CrawlUrl();
			crawUrl.setOriUrl(unvisited);
			if (!visitedFrontier.contain(crawUrl))
				addUrl(unvisitFrontier, crawUrl);
		}
	}

	public void addUrl(Frontier forntier, CrawlUrl crawUrl) throws Exception {
		forntier.putUrl(crawUrl);
	}

	public CrawlUrl dequeueUrl() throws Exception {
		CrawlUrl crawlUrl = null;
		synchronized (unvisitFrontier) {
			crawlUrl = unvisitFrontier.getNext();
			while (true) {

				if (crawlUrl != null) {
					visitedFrontier.putUrl(crawlUrl);
					return crawlUrl;
					// }
				} else {
					MainThread.WAIT_THREAD++;
					// System.out.println(MainThread.WAIT_THREAD);
					if (MainThread.WAIT_THREAD != MainThread.THREAD_NUMBER) {
						MainThread.WAIT_THREAD--;
					} else {
						return null;
					}
				}
			}
		}
	}
}
